<?php defined('SYSPATH') or die("Direkter Zugriff nicht erlaubt.");

class Register_Model extends Model {
	
	private $table;
	
	public function __construct() {
		parent::__construct();
		$this->table = 'users';
	}
	
	public function create($data) {
		if(count($this->db->insert($this->table, $data)) == 0) {
			return false;
		} else {
			return true;
		}
	}
	
	public function getIdByUsername($username) {
		$this->db->where('username', $username);
		$rs = $this->db->get($this->table);
		$hmm = $rs->result_array();
		return $hmm[0];
	}
	
	public function hash_valid($id, $hash) {
		if($this->db->where(array('id' => $id, 'hash' => $hash))->count_records($this->table)
		!=
		0
		) {
			return true;
		} else {
			return false;
		}
	}
	
	public function activate($id, $hash) {
		if($this->hash_valid($id, $hash)) {
			$this->db->update($this->table, array('active' => '1'), array('id' => $id));
			return true;
		} else {
			return false;
		}
	}
	
	public function username_available ($username) {
		if(
			$this->db->where('username',$username)->count_records($this->table)
			!=
			0
		) {
			return false;
		} else {
			return true;
		}
	}
}